package cn.com.nes.site.action.user;

import cn.com.nes.common.util.DBUtils;
import cn.com.nes.site.entity.DataRes;
import cn.com.nes.site.entity.PageData;
import cn.com.nes.site.module.service.CompanyService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.HashMap;

@Controller("company")
@RequestMapping("/company")
public class CompanyAction {

    @Resource
    private CompanyService companyService;

    @RequestMapping(method = RequestMethod.GET, value = "/getCompaniesAroundTheCoordinates")
    public @ResponseBody
    PageData getCompaniesAroundTheCoordinates(HttpServletRequest request,
                                              HttpServletResponse response, ModelMap modelMap) throws UnsupportedEncodingException {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }

        res = companyService.getCompaniesAroundTheCoordinates(reqStatus.getReq());

        return res;
    }

    @RequestMapping(method = RequestMethod.POST, value = "/saveCompanyToExpand")
    public @ResponseBody
    PageData saveCompanyToExpanded(HttpServletRequest request,
                                      HttpServletResponse response, ModelMap modelMap) {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = companyService.saveCompanyToExpand(reqStatus.getReq());
        return res;
    }

    @RequestMapping(method = RequestMethod.GET, value = "/getCompanyToExpand")
    public @ResponseBody
    PageData getCompanyToExpand(HttpServletRequest request,
                                     HttpServletResponse response, ModelMap modelMap) {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = companyService.getCompanyToExpand(reqStatus.getReq());
        return res;
    }

    //获取代理商旗帜分布
    @RequestMapping(method = RequestMethod.GET, value = "/getCompanyFlagMap")
    public @ResponseBody
    PageData getFlagDistributionMap(HttpServletRequest request,
                                HttpServletResponse response, ModelMap modelMap) {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = companyService.getCompanyFlagMap(reqStatus.getReq());
        return res;
    }

    //转换为正式代理商
    @RequestMapping(method = RequestMethod.POST, value = "/convertToFormalCompany")
    public @ResponseBody
    PageData convertToFormalCompany(HttpServletRequest request,
                                    HttpServletResponse response, ModelMap modelMap) {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = companyService.convertToFormalCompany(reqStatus.getReq());
        return res;
    }

    /**
     * 申请成为代理商
     * @param request
     * @param response
     * @param modelMap
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/submitCompanyInformation")
    public @ResponseBody PageData submitCompanyInformation(HttpServletRequest request,
                                                           HttpServletResponse response, ModelMap modelMap) {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = companyService.submitCompanyInformation(reqStatus.getReq());
        return res;
    }

    /**
     * 获取代理商申请列表
     * @param request
     * @param response
     * @param modelMap
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/getAgentIntentApplications")
    public @ResponseBody PageData getAgentIntentApplications(HttpServletRequest request,
                                                           HttpServletResponse response, ModelMap modelMap) {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = companyService.getAgentIntentApplications(reqStatus.getReq());
        return res;
    }

    @RequestMapping(method = RequestMethod.POST, value = "/auditAgentIntentApplication")
    public @ResponseBody PageData auditAgentIntentApplication(HttpServletRequest request,
                                                           HttpServletResponse response, ModelMap modelMap) {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = companyService.auditAgentIntentApplication(reqStatus.getReq());
        return res;
    }

    @RequestMapping(method = RequestMethod.POST, value = "/editCompanyInformation")
    public @ResponseBody PageData editCompanyInformation(HttpServletRequest request,
                                                           HttpServletResponse response, ModelMap modelMap) {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = companyService.editCompanyInformation(reqStatus.getReq());
        return res;
    }

    @RequestMapping(method = RequestMethod.GET, value = "/getAllTargetCompany")
    public @ResponseBody PageData getAllTargetCompany(HttpServletRequest request,
                                                           HttpServletResponse response, ModelMap modelMap) {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = companyService.getAllTargetCompany(reqStatus.getReq());
        return res;
    }

    @RequestMapping(method = RequestMethod.POST, value = "/saveCompanyPromotionCard")
    public @ResponseBody PageData saveCompanyPromotionCard(HttpServletRequest request,
                                                           HttpServletResponse response, ModelMap modelMap) {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = companyService.saveCompanyPromotionCard(reqStatus.getReq());
        return res;
    }


    @RequestMapping(method = RequestMethod.POST, value = "/savePromotionCardDisplay")
    public @ResponseBody PageData savePromotionDisplay(HttpServletRequest request,
                                                           HttpServletResponse response, ModelMap modelMap) {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = companyService.savePromotionDisplay(reqStatus.getReq());
        return res;
    }

    @RequestMapping(method = RequestMethod.POST, value = "/savePromotionBannerDisplay")
    public @ResponseBody PageData savePromotionBannerDisplay(HttpServletRequest request,
                                                           HttpServletResponse response, ModelMap modelMap) {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = companyService.savePromotionBannerDisplay(reqStatus.getReq());
        return res;
    }

    @RequestMapping(method = RequestMethod.POST, value = "/auditCompanyPromotionCard")
    public @ResponseBody PageData auditCompanyPromotionCard(HttpServletRequest request,
                                                           HttpServletResponse response, ModelMap modelMap) {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = companyService.auditCompanyPromotionCard(reqStatus.getReq());
        return res;
    }

    @RequestMapping(method = RequestMethod.GET, value = "/getPromotionBanner")
    public @ResponseBody PageData getPromotionBanner(HttpServletRequest request,
                                                           HttpServletResponse response, ModelMap modelMap) {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = companyService.getPromotionBanner(reqStatus.getReq());
        return res;
    }

    @RequestMapping(method = RequestMethod.GET, value = "/getPromotionCards")
    public @ResponseBody PageData getPromotionCards(HttpServletRequest request,
                                                           HttpServletResponse response, ModelMap modelMap) {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = companyService.getPromotionCards(reqStatus.getReq());
        return res;
    }

    @RequestMapping(method = RequestMethod.GET, value = "/getPromotionCardByCompanyId")
    public @ResponseBody PageData getPromotionCardByCompanyId(HttpServletRequest request,
                                                           HttpServletResponse response, ModelMap modelMap) {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = companyService.getPromotionCardByCompanyId(reqStatus.getReq());
        return res;
    }

    @RequestMapping(method = RequestMethod.GET, value = "/getPromotionDisplayState")
    public @ResponseBody PageData getPromotionDisplayState(HttpServletRequest request,
                                                           HttpServletResponse response, ModelMap modelMap) {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = companyService.getPromotionDisplayState(reqStatus.getReq());
        return res;
    }

    @RequestMapping(method = RequestMethod.POST, value = "/saveCompanyPromotionBanner")
    public @ResponseBody PageData saveCompanyPromotionBanner(HttpServletRequest request,
                                                           HttpServletResponse response, ModelMap modelMap) {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = companyService.saveCompanyPromotionBanner(reqStatus.getReq());
        return res;
    }

    @RequestMapping(method = RequestMethod.POST, value = "/removePromotionDisplay")
    public @ResponseBody PageData removePromotionDisplay(HttpServletRequest request,
                                                           HttpServletResponse response, ModelMap modelMap) {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = companyService.removePromotionDisplay(reqStatus.getReq());
        return res;
    }

    @RequestMapping(method = RequestMethod.GET, value = "/getPromotionBannerByCompanyId")
    public @ResponseBody PageData getPromotionBannerByCompanyId(HttpServletRequest request,
                                                           HttpServletResponse response, ModelMap modelMap)
    {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = companyService.getPromotionBannerByCompanyId(reqStatus.getReq());
        return res;
    }

    @RequestMapping(method = RequestMethod.POST, value = "/auditCompanyPromotionBanner")
    public @ResponseBody PageData auditCompanyPromotionBanner(HttpServletRequest request,
                                                           HttpServletResponse response, ModelMap modelMap)
    {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = companyService.auditCompanyPromotionBanner(reqStatus.getReq());
        return res;
    }

    @RequestMapping(method = RequestMethod.GET, value = "/getCompanyInfo")
    public @ResponseBody PageData getCompanyInfo(HttpServletRequest request,
                                                           HttpServletResponse response, ModelMap modelMap)
    {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = companyService.getCompanyInfo(reqStatus.getReq());
        return res;
    }

    @RequestMapping(method = RequestMethod.GET, value = "/getCertificatedIDCardImageByCompanyId")
    public @ResponseBody PageData getCertificatedIDCardImageByCompanyId(HttpServletRequest request,
                                                           HttpServletResponse response, ModelMap modelMap)
    {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = companyService.getCertificatedIDCardImageByCompanyId(reqStatus.getReq());
        return res;
    }
    
    @RequestMapping(method = RequestMethod.POST, value = "/updateCompany")
    public @ResponseBody PageData updateCompany(HttpServletRequest request,
                                                           HttpServletResponse response, ModelMap modelMap)
    {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = companyService.updateCompany(reqStatus.getReq());
        return res;
    }
    
    @RequestMapping(method = RequestMethod.POST, value = "/getCompanyAllList")
    public @ResponseBody PageData getCompanyAllList(HttpServletRequest request,
                                                           HttpServletResponse response, ModelMap modelMap)
    {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = companyService.getCompanyAllList(reqStatus.getReq());
        return res;
    }

    @RequestMapping(method = RequestMethod.POST, value = "/deleteAllCompany")
    public @ResponseBody
    PageData deleteAllCompany(HttpServletRequest request,
                                              HttpServletResponse response) {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        HashMap<String, Object> param = reqStatus.getReq().getReqData();
        String companyId= (String)param.get("companyId");
        res = companyService.deleteAllCompany(companyId);
        return res;
    }

    //获取代理商分布
    @RequestMapping(method = RequestMethod.GET, value = "/getCompanyMap")
    public @ResponseBody
    PageData getCompanyMap(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws SQLException {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = companyService.getCompanyMap(reqStatus.getReq());
        return res;
    }

    //获取公司详细信息
    @RequestMapping(method = RequestMethod.GET, value = "/getCompanyDetails")
    public @ResponseBody
    PageData getCompanyDetails(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = companyService.getCompanyDetails(reqStatus.getReq());
        return res;
    }

    //获取公司工商变更信息
    @RequestMapping(method = RequestMethod.GET, value = "/getCompanyChange")
    public @ResponseBody
    PageData getCompanyChange(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = companyService.getCompanyChange(reqStatus.getReq());
        return res;
    }

    //获取天眼查联系人
    @RequestMapping(method = RequestMethod.GET, value = "/getCompanyUser")
    public @ResponseBody
    PageData getCompanyUser(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = companyService.getCompanyUser(reqStatus.getReq());
        return res;
    }

}
